<?
	$season_id = 10;

	function GetVirtualChampionshipResultsSql ($matchday_id, $season_id, $start_from, $amount)
	{
		$sql = "SELECT " .
				" TGameUser.id AS user_id" .
				", TGameUser.name AS user_name" .
				", TGameUser.email AS email" .
				", TGameUser.email_sent" .
				", SUM(" .
					" CASE" .
					" WHEN game.matchday_id <> " . $matchday_id . " THEN 0" .
					" WHEN game.host_score IS NULL THEN 0" .
					" WHEN game.hostteam_id = TPrediction.team_id" .
						" AND game.host_score > game.away_score THEN 3" .
					" WHEN game.host_score = game.away_score THEN 1" .
					" WHEN game.awayteam_id = TPrediction.team_id" .
						" AND game.away_score > game.host_score THEN 3" .
					" ELSE 0" .
					" END" .
				") AS matchday_points" .
				", SUM(" .
					" CASE " .
					" WHEN game.matchday_id <> " . $matchday_id . " THEN 0" .
					" WHEN game.host_score IS NULL THEN 0" .
					" WHEN game.hostteam_id = TPrediction.team_id THEN game.host_score " .
					" WHEN game.awayteam_id = TPrediction.team_id THEN game.away_score " .
					" ELSE 0" .
					" END" .
					") AS matchday_goals_scored" .
				", SUM(" .
					" CASE" .
					" WHEN game.matchday_id <> " . $matchday_id . " THEN 0" .
					" WHEN game.away_score IS NULL THEN 0" .
					" WHEN game.hostteam_id = TPrediction.team_id THEN game.away_score" .
					" WHEN game.awayteam_id = TPrediction.team_id THEN game.host_score" .
					" ELSE 0" .
					" END" .
					") AS matchday_goals_allowed" .
				", SUM(" .
					" CASE" .
					" WHEN game.host_score IS NULL THEN 0" .
					" WHEN game.hostteam_id = TPrediction.team_id" .
						" AND game.host_score > game.away_score THEN 3" .
					" WHEN game.host_score = game.away_score THEN 1" .
					" WHEN game.awayteam_id = TPrediction.team_id" .
						" AND game.away_score > game.host_score THEN 3" .
					" ELSE 0" .
					" END" .
				") AS points" .
				", SUM(" .
					" CASE " .
					" WHEN game.host_score IS NULL THEN 0" .
					" WHEN game.hostteam_id = TPrediction.team_id THEN game.host_score " .
					" WHEN game.awayteam_id = TPrediction.team_id THEN game.away_score " .
					" ELSE 0" .
					" END" .
					") AS goals_scored" .
				", SUM(" .
					" CASE" .
					" WHEN game.away_score IS NULL THEN 0" .
					" WHEN game.hostteam_id = TPrediction.team_id THEN game.away_score" .
					" WHEN game.awayteam_id = TPrediction.team_id THEN game.host_score" .
					" ELSE 0" .
					" END" .
					") AS goals_allowed" .
				", SUM(" .
					" CASE" .
					" WHEN game.away_score IS NULL THEN 0" .
				" WHEN game.hostteam_id = TPrediction.team_id THEN game.host_score - game.away_score" .
					" WHEN game.awayteam_id = TPrediction.team_id THEN game.away_score - game.host_score" .
					" ELSE 0" .
					" END" .
					") AS goals_difference" .
			" FROM TGameUser" .
				" LEFT JOIN TPrediction" .
					" ON TGameUser.id = TPrediction.user_id" .
				" LEFT JOIN game" .
					" ON TPrediction.matchday_id = game.matchday_id" .
				" LEFT JOIN matchday ON game.matchday_id = matchday.id" .
			" WHERE matchday.season_id = " . $season_id .
			" AND TGameUser.send_email IS NOT NULL " .
			" AND (game.hostteam_id IS NULL OR game.hostteam_id = TPrediction.team_id OR game.awayteam_id = TPrediction.team_id)" .
			" GROUP BY user_id" .
			" ORDER BY user_id" .
			(($amount != -1) ? " LIMIT " . $start_from . "," . $amount : "");
		return $sql;
	}

	function GetCurrentMatchday ($connect, $season_id, &$md)
	{
		$sql = "SELECT DISTINCT matchday.id, matchday.name, matchday.number" .
			" FROM matchday INNER JOIN game ON matchday.id = game.matchday_id" .
			" WHERE season_id = " . $season_id .
			" AND game.host_score IS NOT NULL" .
			" AND game.away_score IS NOT NULL" .
			" ORDER BY matchday_id DESC" .
			" LIMIT 1";
		$result = mysql_query ($sql, $connect);
		if (False === $result) {
			return "Incorrect query or data!";
		}
		$md = @mysql_fetch_array ($result);
		return False;
	}
?>